package com.mp.approvalreq.util;

import com.alibaba.fastjson.JSONObject;
import com.plusmoney.exception.RpcServerLogicalException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.List;

public class CheckUtil {

    private static Logger logger = Logger.getLogger(CheckUtil.class);


    // 替换文件路径中非法符号:"/"
    public static String replaceNonlicetFilePathCharacter(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return fileName;
        }
        return fileName = fileName.replaceAll("/", "、");
    }

    // 替换sheet表名的非法符号
    public static String replaceNonlicetSheetNameCharacter(String sheetName) {
//     * <li> 0x0000 </li>
//     * <li> 0x0003 </li>
//     * <li> colon (:) </li>
//     * <li> backslash (\) </li>
//     * <li> asterisk (*) </li>
//     * <li> question mark (?) </li>
//     * <li> forward slash (/) </li>
//     * <li> opening square bracket ([) </li>
//     * <li> closing square bracket (]) </li>
        sheetName = sheetName.replaceAll(":", "、").
         replaceAll("\\\\", "、").
         replaceAll("\\*", "").
         replaceAll("\\?", "").
         replaceAll("/", "、").
         replaceAll("\\[", "").
         replaceAll("]", "");
        return sheetName;
    }


    /**
     * 导出自己申请的审批
     */
    public static String checkExportType(JSONObject inputJo) {
        if (inputJo == null) {
            throw new RpcServerLogicalException(1, "导出参数错误,请联系管理员");
        }
        String exportType = inputJo.getString("exportType");

        if (exportType == null || exportType.equals("mySelf")) {
            return exportType;
        } else {
            for (String id : exportType.split(",")) {
                if (!ValidateUtil.isInteger(id)) {
                    throw new RpcServerLogicalException(1, "导出人错误,请联系管理员");
                }
            }
            return exportType;
        }
    }


    /**
     * 获取模板id的集合
     */
    public static List<Integer> checkTemplateIds(JSONObject inputJo) {
        if (inputJo == null || inputJo.getString("templateIds") == null) {
            throw new RpcServerLogicalException(1, "输入模板参数错误,请联系管理员");
        }
        String templateIdStr = inputJo.getString("templateIds");
        List<Integer> templateIds = JSONObject.parseArray(templateIdStr, Integer.class);
        logger.info("CheckUtil templateIds: " + JSONObject.toJSONString(templateIds));
        return templateIds;
    }

    public static List<String> checkDays(JSONObject inputJo) throws Throwable {
        if (inputJo == null || inputJo.getString("days") == null) {
//            throw new RpcServerLogicalException(1,"输入天数错误");
            return null;
        }
        String dateStr = inputJo.getString("days");
        List<String> days = JSONObject.parseArray(dateStr, String.class);
        logger.info("CheckUtil days:" + JSONObject.toJSONString(days));
        if (days.size() > 2) {
            throw new RpcServerLogicalException(1, "输入天数错误");
        }
        if (days.size() == 1) {
            days.add(days.get(0));
        }
        long day = countDays(days.get(0), days.get(1));
        if (day > 200 || day < 0) {
            throw new RpcServerLogicalException(1, "最多可导出200天的数据");
        }
        return days;
    }

    private static long countDays(String startDay, String endDay) throws Throwable {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long startTime = sdf.parse(startDay).getTime();
        long endTime = sdf.parse(endDay).getTime();
        return (endTime - startTime) / (1000 * 60 * 60 * 24);
    }


    /**
     * 获取模板的email集合:即导出到哪里的邮箱
     */
    public static List<String> checkEmails(JSONObject inputJo) {
        if (inputJo == null || inputJo.getString("emails") == null) {
            throw new RpcServerLogicalException(1, "email格式不正确");
        }
        String emailStr = inputJo.getString("emails");
        List<String> emails = JSONObject.parseArray(emailStr, String.class);
        logger.info("CheckUtil emails: " + JSONObject.toJSONString(emails));
        for (String email : emails) {
            email = email.replaceAll(" ", "");                                 // 前端某些客户会输入空格,帮忙去掉
            if (!ValidateUtil.isEmail(email)) {
                throw new RpcServerLogicalException(2, "email格式不正确");
            }
        }
        return emails;
    }


}


